package zhuyun.code.launcheractivity.utils

import okhttp3.Headers
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.util.zip.GZIPInputStream

/**
 * @author: Administrator
 * @date: 2021/11/29
 */
object GZIPUtils {
    fun uncompress(bytes:ByteArray):ByteArray?{
        if(bytes == null || bytes.isEmpty()){
            return null
        }
        val out = ByteArrayOutputStream()
        val `in` = ByteArrayInputStream(bytes)
        val gzipInputStream = GZIPInputStream(`in`)
        val buffer = ByteArray(256)
        var n = gzipInputStream.read(buffer)
        while(n>= 0){
            out.write(buffer,0,n)
            n = gzipInputStream.read(buffer)
        }
        return out.toByteArray()
    }
    fun isGzip(headers: Headers):Boolean{
        var gzip = false
        for(key in headers.names()){
            if(key.equals("Accept-Encoding", true)&& headers.get(key)!!.contains("gzip")||
                    key.equals("Content-Encoding",true)&& headers.get(key)!!.contains("gzip")){
                gzip = true
                break
            }
        }
        return gzip
    }
}